﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    [CustomEditor(typeof(Texture2D)), CanEditMultipleObjects]
    internal class TextureInspector : Editor
    {
        private SerializedProperty m_Aniso;
        private bool m_bShowAlpha;
        private SerializedProperty m_FilterMode;
        [SerializeField]
        private float m_MipLevel;
        [SerializeField]
        protected Vector2 m_Pos;
        private SerializedProperty m_WrapMode;
        private static GUIContent s_AlphaIcon;
        private static GUIContent s_HelpIcon;
        private static GUIContent s_LargeZoom;
        private static GUIStyle s_PreButton;
        private static GUIStyle s_PreLabel;
        private static GUIStyle s_PreSlider;
        private static GUIStyle s_PreSliderThumb;
        private static GUIContent s_RGBIcon;
        private static GUIContent s_SmallZoom;
        private static GUIContent s_TitleSettingsIcon;

        public override string GetInfoString()
        {
            Texture target = base.target as Texture;
            Texture2D t = base.target as Texture2D;
            string str = target.width.ToString() + "x" + target.height.ToString();
            if (QualitySettings.desiredColorSpace == ColorSpace.Linear)
            {
                str = str + " " + TextureUtil.GetTextureColorSpaceString(target);
            }
            bool flag = true;
            bool flag2 = IsNormalMap(target);
            bool flag3 = TextureUtil.DoesTextureStillNeedToBeCompressed(AssetDatabase.GetAssetPath(target));
            bool flag4 = (t != null) && TextureUtil.IsNonPowerOfTwo(t);
            TextureFormat textureFormat = TextureUtil.GetTextureFormat(target);
            flag = !flag3;
            if (flag4)
            {
                str = str + " (NPOT)";
            }
            if (flag3)
            {
                str = str + " (Not yet compressed)";
            }
            else if (!flag2)
            {
                str = str + "  " + TextureUtil.GetTextureFormatString(textureFormat);
            }
            else
            {
                TextureFormat format2 = textureFormat;
                switch (format2)
                {
                    case TextureFormat.ARGB4444:
                        str = str + "  Nm 16 bit";
                        goto Label_015A;

                    case TextureFormat.ARGB32:
                        str = str + "  Nm 32 bit";
                        goto Label_015A;
                }
                if (format2 == TextureFormat.DXT5)
                {
                    str = str + "  DXTnm";
                }
                else
                {
                    str = str + "  " + TextureUtil.GetTextureFormatString(textureFormat);
                }
            }
        Label_015A:
            if (flag)
            {
                str = str + "\n" + EditorUtility.FormatBytes(TextureUtil.GetStorageMemorySize(target));
            }
            return str;
        }

        public override bool HasPreviewGUI()
        {
            return (base.target != null);
        }

        private static void Init()
        {
            s_SmallZoom = EditorGUIUtility.IconContent("PreTextureMipMapLow");
            s_LargeZoom = EditorGUIUtility.IconContent("PreTextureMipMapHigh");
            s_AlphaIcon = EditorGUIUtility.IconContent("PreTextureAlpha");
            s_RGBIcon = EditorGUIUtility.IconContent("PreTextureRGB");
            s_PreButton = "preButton";
            s_PreSlider = "preSlider";
            s_PreSliderThumb = "preSliderThumb";
            s_PreLabel = "preLabel";
        }

        public static bool IsNormalMap(Texture t)
        {
            TextureUsageMode usageMode = TextureUtil.GetUsageMode(t);
            return ((usageMode == TextureUsageMode.NormalmapPlain) || (usageMode == TextureUsageMode.NormalmapDXT5nm));
        }

        private float Log2(float x)
        {
            return (float) (Math.Log((double) x) / Math.Log(2.0));
        }

        protected virtual void OnEnable()
        {
            float realtimeSinceStartup = Time.realtimeSinceStartup;
            if ((Time.realtimeSinceStartup - realtimeSinceStartup) > 1f)
            {
                Debug.LogWarning("Took " + (Time.realtimeSinceStartup - realtimeSinceStartup) + " seconds to create SerializedObject!");
            }
            this.m_WrapMode = base.serializedObject.FindProperty("m_TextureSettings.m_WrapMode");
            this.m_FilterMode = base.serializedObject.FindProperty("m_TextureSettings.m_FilterMode");
            this.m_Aniso = base.serializedObject.FindProperty("m_TextureSettings.m_Aniso");
        }

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = this.m_WrapMode.hasMultipleDifferentValues;
            TextureWrapMode intValue = (TextureWrapMode) this.m_WrapMode.intValue;
            intValue = (TextureWrapMode) EditorGUILayout.EnumPopup(EditorGUIUtility.TempContent("Wrap Mode"), intValue, new GUILayoutOption[0]);
            EditorGUI.showMixedValue = false;
            if (EditorGUI.EndChangeCheck())
            {
                this.m_WrapMode.intValue = (int) intValue;
            }
            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = this.m_FilterMode.hasMultipleDifferentValues;
            UnityEngine.FilterMode selected = (UnityEngine.FilterMode) this.m_FilterMode.intValue;
            selected = (UnityEngine.FilterMode) EditorGUILayout.EnumPopup(EditorGUIUtility.TempContent("Filter Mode"), selected, new GUILayoutOption[0]);
            EditorGUI.showMixedValue = false;
            if (EditorGUI.EndChangeCheck())
            {
                this.m_FilterMode.intValue = (int) selected;
            }
            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = this.m_Aniso.hasMultipleDifferentValues;
            int num = this.m_Aniso.intValue;
            num = EditorGUILayout.IntSlider("Aniso Level", num, 0, 9, new GUILayoutOption[0]);
            EditorGUI.showMixedValue = false;
            if (EditorGUI.EndChangeCheck())
            {
                this.m_Aniso.intValue = num;
            }
            base.serializedObject.ApplyModifiedProperties();
        }

        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type == EventType.Repaint)
            {
                background.Draw(r, false, false, false, false);
                Texture target = base.target as Texture;
                RenderTexture texture2 = target as RenderTexture;
                if ((texture2 != null) && !texture2.IsCreated())
                {
                    texture2.Create();
                }
                float num = !(target is Texture2D) ? 0f : Mathf.Min(this.m_MipLevel, (float) (TextureUtil.CountMipmaps(target as Texture2D) - 1));
                float num2 = Mathf.Min(Mathf.Min((float) (r.width / ((float) target.width)), (float) (r.height / ((float) target.height))), 1f);
                Rect viewRect = new Rect(r.x, r.y, target.width * num2, target.height * num2);
                PreviewGUI.BeginScrollView(r, this.m_Pos, viewRect, "PreHorizontalScrollbar", "PreHorizontalScrollbarThumb");
                float mipMapBias = target.mipMapBias;
                TextureUtil.SetMipMapBiasNoDirty(target, num - this.Log2(((float) target.width) / viewRect.width));
                UnityEngine.FilterMode filterMode = target.filterMode;
                TextureUtil.SetFilterModeNoDirty(target, UnityEngine.FilterMode.Point);
                if (this.m_bShowAlpha)
                {
                    EditorGUI.DrawTextureAlpha(viewRect, target);
                }
                else
                {
                    EditorGUI.DrawPreviewTexture(viewRect, target);
                }
                TextureUtil.SetMipMapBiasNoDirty(target, mipMapBias);
                TextureUtil.SetFilterModeNoDirty(target, filterMode);
                this.m_Pos = PreviewGUI.EndScrollView();
                if (num != 0f)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 20f), "Mip " + num);
                }
            }
        }

        public override void OnPreviewSettings()
        {
            Init();
            Texture target = base.target as Texture;
            Texture2D t = base.target as Texture2D;
            bool flag = true;
            bool flag2 = false;
            bool flag3 = true;
            int a = 1;
            if (t != null)
            {
                flag2 = true;
                flag3 = false;
                foreach (Texture2D textured2 in base.targets)
                {
                    TextureFormat format = textured2.format;
                    if (!TextureUtil.IsAlphaOnlyTextureFormat(format))
                    {
                        flag2 = false;
                    }
                    if (TextureUtil.HasAlphaTextureFormat(format))
                    {
                        flag3 = true;
                    }
                    a = Mathf.Max(a, TextureUtil.CountMipmaps(t));
                }
            }
            if (flag2)
            {
                this.m_bShowAlpha = true;
                flag = false;
            }
            else if (!flag3)
            {
                this.m_bShowAlpha = false;
                flag = false;
            }
            if (flag && !IsNormalMap(target))
            {
                this.m_bShowAlpha = GUILayout.Toggle(this.m_bShowAlpha, !this.m_bShowAlpha ? s_RGBIcon : s_AlphaIcon, s_PreButton, new GUILayoutOption[0]);
            }
            GUI.enabled = a != 1;
            GUILayout.Box(s_SmallZoom, s_PreLabel, new GUILayoutOption[0]);
            GUI.changed = false;
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.MaxWidth(64f) };
            this.m_MipLevel = Mathf.Round(GUILayout.HorizontalSlider(this.m_MipLevel, (float) (a - 1), 0f, s_PreSlider, s_PreSliderThumb, options));
            GUILayout.Box(s_LargeZoom, s_PreLabel, new GUILayoutOption[0]);
            GUI.enabled = true;
        }

        public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return null;
            }
            Texture target = base.target as Texture;
            PreviewHelpers.AdjustWidthAndHeightForStaticPreview(target.width, target.height, ref width, ref height);
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            RenderTexture active = RenderTexture.active;
            RenderTexture dest = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
            Material materialForSpecialTexture = EditorGUI.GetMaterialForSpecialTexture(target);
            if (materialForSpecialTexture != null)
            {
                if (Unsupported.IsDeveloperBuild())
                {
                    materialForSpecialTexture = new Material(materialForSpecialTexture);
                }
                Graphics.Blit(target, dest, materialForSpecialTexture);
            }
            else
            {
                Graphics.Blit(target, dest);
            }
            RenderTexture.active = dest;
            Texture2D textured = new Texture2D(width, height, TextureFormat.RGB24, false);
            textured.ReadPixels(new Rect(0f, 0f, (float) width, (float) height), 0, 0);
            textured.Apply();
            RenderTexture.ReleaseTemporary(dest);
            EditorGUIUtility.SetRenderTextureNoViewport(active);
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            if ((materialForSpecialTexture != null) && Unsupported.IsDeveloperBuild())
            {
                UnityEngine.Object.DestroyImmediate(materialForSpecialTexture);
            }
            return textured;
        }
    }
}

